package com.yx.learn.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.yx.learn.dao.PositionDao;
import com.yx.learn.domain.Position;



@Repository("positionDao")
public class PositionDaoImpl implements PositionDao{
	
	@Autowired
	@Qualifier("hibernateTemplate")
	private HibernateTemplate hibernateTemplate;

	public void save(Position position) throws Exception{
		// TODO Auto-generated method stub
		hibernateTemplate.save(position);
	}

	public void delete(Position position) throws Exception{
		// TODO Auto-generated method stub
		hibernateTemplate.delete(position);
		
	}

	public void update(Position position) throws Exception{
		// TODO Auto-generated method stub

		hibernateTemplate.merge(position);
	}

	public List<Position> findAll() throws Exception{
		// TODO Auto-generated method stub

		String hql = "From Position";
		List<Position> positions = hibernateTemplate.find(hql);
		return positions;
	}

	public List<Position> findPositions(String hql, int pageNumber, int pageSize) throws Exception{
		// TODO Auto-generated method stub

		Query query = hibernateTemplate.getSessionFactory().getCurrentSession()
				.createQuery(hql);
		query.setFirstResult((pageNumber - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<Position> positions = query.list();
		return positions;
	}

	public Position findById(int id) throws Exception{
		// TODO Auto-generated method stub
		String hql = "From Position where id=?";
		List<Position> positions = hibernateTemplate.find(hql,id);
		return positions.get(0);
	}
}

